package cn.com.zcode.email.service;

import java.util.Date;
import java.util.Map;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.log4j.Logger;

import cn.com.zcode.email.dao.EmailDao;
import cn.com.zcode.email.model.EmailAutherticator;
import cn.com.zcode.email.model.EmailFactory;
import cn.com.zcode.email.model.XmlEmailTemp;

public class EmailServiceImpl implements EmailService {
	private Logger logger = Logger.getLogger(EmailServiceImpl.class);
	private EmailDao emailDao;
	private String smtp;
	private String username;
	private String password;
	private String from;
	private boolean needAuth;
	public EmailFactory emailFactory;

	@Override
	public boolean send(String to, String cc, String temKey, Map<String, Object> datas) {
		XmlEmailTemp temp = emailDao.getTemplate(temKey);
		String title = emailFactory.getTitle(temp, datas);
		String body = emailFactory.getBody(temp, datas);
		return this.send(to, cc, title, body);
	}

	public boolean send(String to, String cc, String title, String body) {
		if (to != null && !to.isEmpty()) {
			try {
				Authenticator auth = new EmailAutherticator(username, password);
				Properties props = new Properties();
				props.put("mail.smtp.host", smtp);
				props.put("mail.smtp.auth", "true");
				Session session = Session.getDefaultInstance(props, auth);
				session.setDebug(false);
				MimeMessage message = new MimeMessage(session);
				message.setSubject(title);
				message.setText(body);
				message.setSentDate(new Date());
				Address address = new InternetAddress(from);
				message.setFrom(address);
				Address toAddress = new InternetAddress(to);
				message.addRecipient(Message.RecipientType.TO, toAddress);
				Transport.send(message);
				logger.info("Send Successfully:to=" + to);
				return true;
			} catch (Exception e) {
				throw new RuntimeException("fail to send email");
			}
		}
		return false;
	}

	public String getSmtp() {
		return smtp;
	}

	public void setSmtp(String smtp) {
		this.smtp = smtp;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getFrom() {
		return from;
	}

	public void setFrom(String from) {
		this.from = from;
	}

	public boolean isNeedAuth() {
		return needAuth;
	}

	public void setNeedAuth(boolean needAuth) {
		this.needAuth = needAuth;
	}

	public EmailDao getEmailDao() {
		return emailDao;
	}

	public void setEmailDao(EmailDao emailDao) {
		this.emailDao = emailDao;
	}

	public EmailFactory getEmailFactory() {
		return emailFactory;
	}

	public void setEmailFactory(EmailFactory emailFactory) {
		this.emailFactory = emailFactory;
	}

}
